<%@page import="org.esgi.model.interfaces.IQuestion"%>
<%@page import="org.esgi.model.interfaces.IAnswer"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<%@page import="org.esgi.model.interfaces.IForm"%>
<%@page import="org.esgi.dao.FormDao"%>
<%@page import="java.util.List"%>
<%@page import="java.io.File"%>

<% IForm form = (IForm)request.getAttribute("form"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Form detail</title>
<%@include file="headers.jsp"%>
<script LANGUAGE="JavaScript">

var lastQuestion = 0;
function didClickOnAnswer(questionId){
	var selectedAnswer = checkedRadioBtn("questions"+questionId);
	if(undefined != selectedAnswer){
		switchPanel(questionId, selectedAnswer);
	}
}

function switchPanel(oldPanel, newPanel){
	lastQuestion = oldPanel;
	var actualPanel = document.getElementById("questionPanel" + oldPanel);
	actualPanel.className = "questionPanel hidden";
	
	var newPanel = document.getElementById("questionPanel" + newPanel);
	newPanel.className = "questionPanel";
}

function checkedRadioBtn(sGroupName)
{   
    var group = document.getElementsByName(sGroupName);

    for ( var i = 0; i < group.length; i++) {
        if (group.item(i).checked) {
            return group.item(i).value;
        } else if (group[0].type !== 'radio') {
            //if you find any in the group not a radio button return null
            return null;
        }
    }
}

function back(panel){
	switchPanel(panel, lastQuestion);
}
</script>

<style type="text/css">
.hidden{
	display: none;
}
</style>
</head>
<body>
<%
	out.write("<h1>" + form.getName() + "</h1>");
	int i = 0;
	
	for(IQuestion q : form.getQuestions()){
		
		String hidden = (0 != i) ? "hidden" : "";
		out.write("<div class='questionPanel "+ hidden +"' id='questionPanel"+q.getId()+"'>");
		out.write("<h2>" + q.getText() + "</h2>");
	
		for(IAnswer a : q.getAnswers()){
			out.write("<br /><input type='radio' name='questions"+q.getId()+"' value='"+ a.getNextQuestionId() +"'>" + a.getText());
		}
		if (0 != i){
			out.write("<br /><input type='button' value='Back' onclick='back("+q.getId()+");'>");	
		}
		out.write("<br /><input type='button' value='Answer' onclick='didClickOnAnswer("+q.getId()+");'>");
		out.write("</div>");	
		i++;
	}
	
%>
</body>
</html>